package my.jwds.p29;

import java.util.LinkedList;

public class Divide {


    public static void main(String[] args) {
        Divide obj = new Divide();
        System.out.println(obj.divide(10,3));
    }

    
    public int divide(int dividend, int divisor) {
        boolean negative = dividend <0 && divisor >0|| dividend >0 && divisor <0;
        dividend = dividend>0?-dividend:dividend;
        divisor = divisor>0?-divisor:divisor;
        int count = 0;
        LinkedList<Long> multiples = new LinkedList<>();
        LinkedList<Long> divisors = new LinkedList<>();
        multiples.addFirst(1l);
        divisors.addFirst((long) divisor);
        while(dividend < divisors.getLast()){
            multiples.add(multiples.getLast()+multiples.getLast());
            divisors.add(divisors.getLast()+divisors.getLast());
        }
        while(multiples.size() != 0){
            if (dividend <= divisors.getLast()){
                dividend -= divisors.getLast();
                count -= multiples.getLast();
            }
            multiples.removeLast();
            divisors.removeLast();
        }
        return negative ? count:count == Integer.MIN_VALUE ? Integer.MAX_VALUE : -count;
    }
}
